﻿using System;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using AstekServis.Web.Data;

namespace AstekServis.Views
{
    public partial class AracListesi : ChildWindow
    {
        public string Filtre { get; set; }
        public AracBilgi SecilenArac { get; set; }
        public StokBilgi SecilenStok { get; set; }

        AsTekDomainContext context = new AsTekDomainContext();
        public enum ListeTipleri
        {
            Arac, Stok
        }
        public ListeTipleri ListeTipi { get; set; }

        public AracListesi()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(AracListesi_Loaded);
        }

        void AracListesi_Loaded(object sender, RoutedEventArgs e)
        {
            switch (ListeTipi)
            {
                case ListeTipleri.Arac:
                    LoadArac();
                    break;
                case ListeTipleri.Stok:
                    LoadStok();
                    break;
                default:
                    LoadArac();
                    break;
            }
        }

        private void LoadArac()
        {
            EntityQuery<AracBilgi> qry = null;
            if (!string.IsNullOrEmpty(Filtre))
            {
                qry = context.GetAracBilgileriQuery().Where(x => x.AracKodu.StartsWith(Filtre) || x.Plaka.StartsWith(Filtre) || x.GrupAdi.StartsWith(Filtre) || x.FirmaAdi.StartsWith(Filtre)).OrderBy(x => x.Plaka);
            }
            else
            {
                qry = context.GetAracBilgileriQuery().OrderBy(x => x.Plaka);
            }

            LoadOperation<AracBilgi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<AracBilgi> op = ss as LoadOperation<AracBilgi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.AddColumn("AracKodu");
                    agDataGrid1.AddColumn("Plaka");
                    agDataGrid1.DataSource = op.Entities;
                }
            };
        }

        private void LoadStok()
        {
            EntityQuery<StokBilgi> qry = null;
            if (!string.IsNullOrEmpty(Filtre))
            {
                qry = context.GetStokBilgileriQuery().Where(x => x.Adi.StartsWith(Filtre) || x.Kodu.StartsWith(Filtre));
            }
            else
            {
                qry = context.GetStokBilgileriQuery();
            }

            LoadOperation<StokBilgi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<StokBilgi> op = ss as LoadOperation<StokBilgi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.AddColumn("Kodu");
                    agDataGrid1.AddColumn("Adi");
                    agDataGrid1.DataSource = op.Entities;
                }
            };
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            GridSec();
            this.DialogResult = true;
        }

        private void GridSec()
        {
            if (agDataGrid1.FocusedRowHandle > -1)
            {
                switch (ListeTipi)
                {
                    case ListeTipleri.Stok:
                        SecilenStok = (StokBilgi)agDataGrid1.GetDataRow(agDataGrid1.FocusedRowHandle);
                        break;
                    case ListeTipleri.Arac:
                    default:
                        SecilenArac = (AracBilgi)agDataGrid1.GetDataRow(agDataGrid1.FocusedRowHandle);
                        break;
                }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void agDataGrid1_MouseDoubleClick(object sender, EventArgs e)
        {
            GridSec();
            this.DialogResult = true;
        }
    }
}

